<?php
/**
 * Add the ability to access model-classes with ArrayAccess style 
 * and lazy-loading function
 * 
 * Zend_Registry component used to store class instancess
 */
abstract class Wise_Controller_Abstract extends Zend_Controller_Action implements ArrayAccess 
{
	/**
	 * All user related data stored here 
	 * 
	 * @var Wise_User_Session
	 */
	protected $User = null;
	
	/**
	 * Initialized actoin controller required data
	 */
	public function init()
	{
		$this->User = Wise_User_Session::getInstance();
	}
	
	/**
	 * ArrayAccess is exists functionality 
	 * @param offset 
	 * @see ArrayAccess::offsetExists()
	 */
	public function offsetExists($offset)
	{
		return Zend_Registry::isRegistered('Wise_Controller_Models_' . $offset);
	}
	
	/**
	 * ArrayAccess get item functionality
	 * as we have "lazy-loading" implemented here 
     * just use self::offsetGet() to instantiate 
     * and receive class instance 
	 * 
	 * @param offset 
	 * @see ArrayAccess::offsetGet()
	 */
	public function offsetGet($offset)
	{
		if ($this->offsetExists($offset)) {
			return Zend_Registry::get('Wise_Controller_Models_' . $offset);
		} else {
			$model = new $offset();
			Zend_Registry::set('Wise_Controller_Models_' . $offset, $model);
			return $model;
		}
		
		return false;
	}
	
	/**
	 * ArrayAccess set item functionality
	 * 
	 * this is empty for now, we don't need this functionality
	 * 
	 * @param offset 
	 * @param value 
	 * @see ArrayAccess::offsetSet()
	 */
	public function offsetSet($offset, $value)
    {
	   	// do nothing
	}
	
	/**
	 * ArratAccess unset item functionality
	 * @param offset 
	 * @see ArrayAccess::offsetUnset()
	 */
	public function offsetUnset($offset)
	{
	   	// do nothing
	}
}